//
//  PlayerLoader.cpp
//  TrackingSim
//
//  Created by Garrett Manka on 2/12/12.
//  Copyright (c) 2012. All rights reserved.
//

#include <fstream>
#include <string>
#include <sim/PlayerLoader.h>

void PlayerLoader::load(const std::string& path, std::vector<Sim::Player*>* objects)
{
    std::ifstream player_file( path.c_str(), std::ifstream::in);
    
    if( player_file.is_open() )
    {
        std::string attribute, name, mesh;
        double x , y , z;
        Sim::Vector pos, vel, up;
        bool name_set = false;
        bool pos_set = false;
        bool vel_set = false;
        bool up_set = false;
        bool mesh_set = false;
        
        while( !player_file.eof() )
        {
            player_file >> attribute;
            
            if ( attribute == "name" )
            {
                player_file >> name;
                name_set = true;
            }
            else if( attribute == "mesh" )
            {
                player_file>> mesh;
                mesh_set = true;
            }
            else if( attribute == "pos" )
            {
                player_file >> x >> y >> z;
                pos.x( x );
                pos.y( y );
                pos.z( z );
                pos_set = true;
            }
            else if( attribute == "vel" )
            {
                player_file >> x >> y >> z;
                vel.x( x );
                vel.y( y );
                vel.z( z );
                vel_set = true;            
            }
            else if( attribute == "up" )
            {
                player_file >> x >> y >> z;
                up.x( x );
                up.y( y );
                up.z( z );
                up_set = true;
            }
            else
            {
                player_file.ignore(1024, '\n');
            }
            
            if( name_set && mesh_set && pos_set && vel_set && up_set )
            {
                Sim::Player* new_player = new Sim::Player(name,pos,vel,up);
                new_player->setMesh(mesh);
                
                objects->push_back( new_player);
                
                name_set = false;
                mesh_set = false;
                pos_set = false;
                vel_set = false;
                up_set = false;
            }
        }
    }
}